package com.example.pixelyear.ui.base;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


import com.example.pixelyear.R;
import com.example.pixelyear.view.MyProgressLoading;

import org.jetbrains.annotations.NotNull;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

/**
 * Package    : com.example.baseutils.BaseUI
 * When I wrote this, only The jade emperor and I understood what I was doing, Now God only knows
 * ClassName  : BaseFragment
 * Description: $所有Fragment基类
 * Author     : 坐怀灬不乱
 * Date       : 2018/12/22 14:59
 */
public abstract class BaseFragment extends Fragment {
    protected Activity mActivity;
    protected View rootView;
    protected MyProgressLoading loading;
    public boolean isVisible = false;
    protected boolean isViewLoadSucc = false;

    @Nullable
    @Override
    public View onCreateView(@NotNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mActivity = getActivity();
        FirstLoad();
        if (rootView == null) {
            rootView = inflater.inflate(provideContentViewId(), container, false);
            loading = new MyProgressLoading(mActivity, R.style.DialogStyle);
        } else {
            ViewGroup parent = (ViewGroup) rootView.getParent();
            if (parent != null) {
                parent.removeView(rootView);
            }
        }
        return rootView;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        isViewLoadSucc = true;
        initView(rootView, savedInstanceState);
        initData();
        initListener();
    }

    //在inflate布局文件之前调用
    protected void FirstLoad() {
    }


    public void setStateBarColor(Activity activity) {
    }

    /**
     * 得到当前界面的布局文件id(由子类实现)
     *
     * @return 布局ID
     */
    public abstract int provideContentViewId();

    /**
     * 初始化View：findViewById
     * 参数:view,通过View查找控件
     */
    public abstract void initView(View view, @Nullable Bundle savedInstanceState);

    /**
     * 初始化数据
     */
    public abstract void initData();

    /**
     * 初始化监听：点击监听、设置适配器、设置条目点击监听
     */
    public abstract void initListener();


    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser && isViewLoadSucc) {
            isVisible = true;
            canLoadData();
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        isVisible = true;
        if (isViewLoadSucc) {
            canLoadData();
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        isVisible = false;
    }

    public void canLoadData() {
    }
}
